package net.wangds.yunz.auth.filter;

import net.wangds.yunz.auth.handler.LoginAuthenticationFailureHandler;
import net.wangds.yunz.auth.handler.LoginAuthenticationSuccessHandler;
import net.wangds.yunz.auth.service.AuthService;
import net.wangds.yunz.auth.viewmodel.form.LoginForm;
import net.wangds.iocomp.utils.StreamUtils;
import net.wangds.jwt.comp.JwtTokenComp;
import net.wangds.utils.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;

@Component
public class JwtLoginFilter extends UsernamePasswordAuthenticationFilter {

    @Autowired
    JwtTokenComp mmCmpJwtToken;

    @Autowired
    private AuthService mmServM10Auth;

    ThreadLocal<LoginForm> localForm = new ThreadLocal<>();

    /**
     * Jwt登录过滤器.
     * @param am 认证管理器.
     * @param onSuccess 登录成功处理过程.
     * @param onFailure 登录失败处理过程.
     */
    public JwtLoginFilter(
            AuthenticationManager am,
            LoginAuthenticationSuccessHandler onSuccess,
            LoginAuthenticationFailureHandler onFailure
    ) {
        super.setAuthenticationManager(am);
        super.setFilterProcessesUrl("/auth/login");
        super.setAuthenticationSuccessHandler(onSuccess);
        super.setAuthenticationFailureHandler(onFailure);
    }

    @Override
    protected String obtainUsername(HttpServletRequest request) {
        try(InputStream is=request.getInputStream()){
            String json = StreamUtils.readAsString(is, "UTF8");
            json = StringUtils.trim(json);
            //LogHelper.dev("login json:"+json+"|");
            LoginForm form = JsonUtils.fromJsonStr(json, LoginForm.class);
            localForm.set(form);
            //LogHelper.dev("获得用户名:"+form.getLoginname());
            //LogHelper.dev("获得登录验证码:"+form.getVerifyCode());
            //LogHelper.dev("获得登录令牌:"+form.getVerifyToken());

            //String loginname = form.getLoginname();
            //if(!form.isConsole()) {
                //APP登录的时候检验验证码
                //boolean res_verify = mmServM10Auth.validateVerifyCode(form.getVerifyToken(), form.getVerifyCode());
                //if (!res_verify) {
                    // throw new AuthenticationServiceException("验证码错误");
                //}
            //}else {
                //控制台登录不用校验验证码
                //LogHelper.dev(()->"用户:\""+loginname+"\"登录后台,"+ DateUtils.toDefaultString(new Date()));
            //}
            //return loginname;

            return form.getLoginname();
        } catch (IOException e) {
            throw new UsernameNotFoundException("无法获取用户名",e);
        }
    }

    @Override
    protected String obtainPassword(HttpServletRequest request) {
        return localForm.get().getPassword();
    }


}
